package controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.*;
import java.util.Map;

@Controller
@RequestMapping("/param")
public class ParamController {

    @RequestMapping("/f1")
    public void f1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("ParamController.f1()");
        System.out.println("request = " + request + ", response = " + response);

        System.out.println(request.getRemoteAddr());

        PrintWriter out=response.getWriter();

        out.print("<h1>Hello</h1>");

        out.flush();
    }

    @RequestMapping("/f2")
    public void f2(InputStream in){
        System.out.println("ParamController.f2()");
        System.out.println("in = " + in);
    }

    @RequestMapping("/f3")
    public void f3(OutputStream out){
        System.out.println("ParamController.f3()");
        System.out.println("out = " + out);
    }

    @RequestMapping("/f4")
    public void f4(Reader in){
        System.out.println("ParamController.f4()");
        System.out.println("in = " + in);
    }

    @RequestMapping("/f5")
    public void f5(Writer out){
        System.out.println("ParamController.f5()");
        System.out.println("out = " + out);
    }

    @RequestMapping("/f6")
    public void f6(PrintWriter out){
        System.out.println("ParamController.f6()");
        System.out.println("out = " + out);
        out.print("<h1>Hello</h1>");
        out.flush();
    }

    @RequestMapping("/f7")
    public void f7(WebRequest webRequest, NativeWebRequest nativeWebRequest){
        System.out.println("ParamController.f7()");
        System.out.println("webRequest = " + webRequest + ", nativeWebRequest = " + nativeWebRequest);

        webRequest.setAttribute("attrName","attrValue",WebRequest.SCOPE_REQUEST);
        webRequest.setAttribute("attrName","attrValue",WebRequest.SCOPE_SESSION);

        System.out.println(webRequest.getAttribute("attrName",WebRequest.SCOPE_REQUEST));
        System.out.println(webRequest.getAttribute("attrName",WebRequest.SCOPE_SESSION));

        HttpServletRequest request=nativeWebRequest.getNativeRequest(HttpServletRequest.class);
        HttpServletResponse response=nativeWebRequest.getNativeResponse(HttpServletResponse.class);

        System.out.println("request = " + request);
        System.out.println("response = " + response);

    }

    @RequestMapping("/f8")
    public void f8(HttpSession session){
        System.out.println("ParamController.f8()");
        System.out.println("session = " + session);
    }

    @RequestMapping("/f9")
    public String f9(Model model, ModelMap modelMap, Map map){
        System.out.println("ParamController.f9()");
        System.out.println("model = " + model + ", modelMap = " + modelMap + ", map = " + map);

        model.addAttribute("model","model value");

        modelMap.addAttribute("modelMap","mpdelMap value");
        modelMap.put("modelMap","mpdelMap value");

        map.put("map","map value");
        return "model";
    }

    @RequestMapping("/f10")
    public String f10(){
        System.out.println("ParamController.f10()");
        return "redirect:/showModel?name=abc";
    }

    @RequestMapping("/f11")
    public String f11(Map map){
        System.out.println("ParamController.f11()");
        map.put("name","aaa");
        return "redirect:/showModel";
    }

    @RequestMapping("/f12")
    public String f12(RedirectAttributes redirectAttributes){
        System.out.println("ParamController.f12()");

        redirectAttributes.addAttribute("name","bbb");
        redirectAttributes.addFlashAttribute("name","ccc");

        return "redirect:/showModel";
    }

    @RequestMapping("/f13")
    public void f13(String name){
        System.out.println("ParamController.f13()");
        System.out.println("name = " + name);
    }

    @RequestMapping("/f14")
    public void f14(@RequestParam String name){
        System.out.println("ParamController.f14()");
        System.out.println("name = " + name);
    }

    @RequestMapping("/f15")
    public void f15(@RequestParam("username") String name){
        System.out.println("ParamController.f15()");
        System.out.println("name = " + name);
    }

    @RequestMapping("/f16")
    public void f16(@RequestParam(value = "name",required = false) String name){
        System.out.println("ParamController.f16()");
        System.out.println("name = " + name);
    }

    @RequestMapping("/f17")
    public void f17(@RequestParam(value = "name",defaultValue = "abc") String name){
        System.out.println("ParamController.f17()");
        System.out.println("name = " + name);
    }

    @RequestMapping("/f18/{name}")
    public void f18(@PathVariable String name){
        System.out.println("ParamController.f18()");
        System.out.println("name = " + name);
    }

    @RequestMapping("/f19/{name}")
    public void f19(@PathVariable("name") String name){
        System.out.println("ParamController.f19()");
        System.out.println("name = " + name);
    }

    @RequestMapping("/f20/{username}")
    public void f20(@PathVariable("username") String name){
        System.out.println("ParamController.f20()");
        System.out.println("name = " + name);
    }

    @RequestMapping("/f21")
    public void f21(@CookieValue("JSESSIONID") String sessionId){
        System.out.println("ParamController.f21()");
        System.out.println("sessionId = " + sessionId);
    }

    @RequestMapping("/f22")
    public void f22(@RequestHeader("User-Agent") String userAgent){
        System.out.println("ParamController.f22()");
        System.out.println("userAgent = " + userAgent);
    }

    @RequestMapping(value = "/f23",method = RequestMethod.POST)
    public void f23(@RequestBody String body){
        System.out.println("ParamController.f23()");
        System.out.println("body = " + body);
    }

    @RequestMapping("/f24")
    public void f24(@Value("abc") String name){
        System.out.println("ParamController.f24()");
        System.out.println("name = " + name);
    }

    @RequestMapping("/f25")
    public void f25(@Value("#{new String('abc').toUpperCase()}") String name){
        System.out.println("ParamController.f25()");
        System.out.println("name = " + name);
    }

    @RequestMapping("/f26")
    public void f26(@Value("#{someBean.str}") String str){
        System.out.println("ParamController.f26()");
        System.out.println("str = " + str);
    }

    @RequestMapping("/f27")
    public void f27(@Value("#{T(Math).random()}") double num){
        System.out.println("ParamController.f27()");
        System.out.println("num = " + num);
    }

    @RequestMapping("/f28")
    public void f28(@Value("#{systemProperties['java.vm.version']}") String jvmVerson){
        System.out.println("ParamController.f28()");
        System.out.println("jvmVerson = " + jvmVerson);
    }
}





















